package com.dxy.df.modules.application;

import com.dxy.df.constants.ErrorEnum;
import com.dxy.df.core.exception.BizException;
import com.dxy.df.core.shiro.ShiroUtils;
import com.dxy.df.modules.bean.entity.UserPublishGiveRiceEntity;
import com.dxy.df.modules.bean.put.UserAcceptRiceRecordPut;
import com.dxy.df.modules.bean.put.UserPublishAcceptRicePut;
import com.dxy.df.modules.bean.put.UserPublishGiveRicePut;
import com.dxy.df.modules.bean.query.UserPublishGiveRiceQuery;
import com.dxy.df.modules.bean.vo.UserPublishAcceptRiceVo;
import com.dxy.df.modules.service.UserAcceptRiceRecordService;
import com.dxy.df.modules.service.UserPublishAcceptRiceService;
import com.dxy.df.modules.service.UserPublishGiveRiceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author wangsheng@dxy.cn
 * @date 2019-10-25
 **/
@Component
public class GivenRiceApplication {

    private static final Logger LOGGER = LoggerFactory.getLogger(GivenRiceApplication.class);

    private UserPublishGiveRiceService userPublishGiveRiceService;

    private UserPublishAcceptRiceService userPublishAcceptRiceService;

    private UserAcceptRiceRecordService userAcceptRiceRecordService;


    public GivenRiceApplication(UserPublishGiveRiceService userPublishGiveRiceService,
                                UserPublishAcceptRiceService userPublishAcceptRiceService,
                                UserAcceptRiceRecordService userAcceptRiceRecordService) {
        this.userPublishGiveRiceService = userPublishGiveRiceService;
        this.userPublishAcceptRiceService = userPublishAcceptRiceService;
        this.userAcceptRiceRecordService = userAcceptRiceRecordService;
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveUserPublishGivenInfo(UserPublishGiveRicePut giveRicePut) {


        UserPublishGiveRiceQuery giveRiceQuery = new UserPublishGiveRiceQuery();
        giveRiceQuery.setUserId(ShiroUtils.getUserAccount());
        giveRiceQuery.setCreateTime(new Date());
        UserPublishGiveRiceEntity userPublishGiveRiceEntity = userPublishGiveRiceService.queryObject(giveRiceQuery);
        if (null != userPublishGiveRiceEntity) {
            throw new BizException(ErrorEnum.HAD_GIVEN_RICE_ASK);
        }

        UserPublishGiveRiceEntity giveRiceEntity = userPublishGiveRiceService.save(giveRicePut);

        //发布出饭的时候，检查是否有匹配，如果有匹配直接修改本次自动出饭的状态（已被收）
        if (this.givenMatchAccess(ShiroUtils.getUserAccount(), giveRiceEntity.getId(),
                giveRicePut.getRiceName())) {
            LOGGER.info("发布出饭，被自动匹配上....");
            UserPublishGiveRicePut givenRicePut = new UserPublishGiveRicePut();
            givenRicePut.setId(giveRiceEntity.getId());
            givenRicePut.setBizStatus(1);
            userPublishGiveRiceService.update(givenRicePut);
        }
    }


    /**
     * 新的出饭是否匹配已有的收饭
     * 发布出饭的时候，检查是否有匹配，如果有匹配直接修改本次自动出饭的状态（已被收）
     *
     * @param userAccount
     * @param givenRiceId
     * @return
     */
    public boolean givenMatchAccess(String userAccount, Integer givenRiceId, String riceName) {

        LOGGER.info("------------start 查询最早匹配的【收】饭信息....");
        //查询最早匹配的收饭信息
        UserPublishAcceptRiceVo userPublishAcceptRiceVo = userPublishAcceptRiceService.querySuitInfo(riceName);
        if (null != userPublishAcceptRiceVo) {
            //增加对接信息
            UserAcceptRiceRecordPut recordPut = new UserAcceptRiceRecordPut();
            recordPut.setAcceptRiceId(userPublishAcceptRiceVo.getId());
            recordPut.setGiveRiceId(givenRiceId);
            recordPut.setAcceptUserId(userPublishAcceptRiceVo.getUserId());
            userAcceptRiceRecordService.save(recordPut);

            //修改收饭状态
            UserPublishAcceptRicePut acceptRicePut = new UserPublishAcceptRicePut();
            acceptRicePut.setId(userPublishAcceptRiceVo.getId());
            acceptRicePut.setBizStatus(1);
            userPublishAcceptRiceService.update(acceptRicePut);

            return true;
        }
        return false;
    }


}
